<?php

namespace App\Http\Controllers\Api;

use App\Model\UserModel;
use App\Model\UserLoginModel;
use App\Model\UserWalletModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Cache;


class PartyController extends Controller
{
	//获取信息
	public function qq_info(){
		//openid为空
		if(!request()->openid){
			return ['code'=>0,'msg'=>retStrInfo(115)];
		}
		$type = request()->type;
		//苹果第三方登录
		if($type == 3){
			$party_user = UserLoginModel::where('openid',request()->openid)->first();
			DB::beginTransaction();
			try {
				$users = UserModel::where('id',$party_user['uid'])->first();
				if(!$party_user){
					do {
						$invite_code = getRandomString(6);
					} while (UserModel::where('invite_code', $invite_code)->first());
					do {
						//昵称
						$user_cy = $invite_code;
					} while (UserModel::where('user', $user_cy)->first());
		
					$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
					if (preg_match($auth_name, $user_cy)) {
						$pin = app('pinyin');
						$pinyin =$pin->abbr($user_cy);
					}else{
						$pinyin = $user_cy;
					}
					//首字母
					$head = pinying($pinyin);
		
					$user = UserModel::create([
						// 'email' => $email,
						'user' => $user_cy,
						'invite_code' => $invite_code,
						'login_type' => 2,
						'head' => $head,
					]);
					$user->generateToken();
					UserWalletModel::create([
						'uid'=>$user->id,
					]);
					UserLoginModel::create([
						'uid' => $user->id,
						'openid' => request()->openid,
						'type' => 3,
					]);
		
					$qrCode = new \Endroid\QrCode\QrCode('http://' . request()->getHost() . '/invite_own?invite=' . $user['invite_code']);
		
					$qrCode->setSize(360);
					$qrCode->writeFile(public_path() . "/upload/invite/{$user['id']}.png");
					$bigImgPath = public_path() . '/upload/invite/invite.png';
					$qCodePath = public_path() . "/upload/invite/{$user['id']}.png";
					$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
					$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
					list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
					imagecopymerge($bigImg, $qCodeImg, 150, 50, 0, 0, $qCodeWidth, $qCodeHight, 100);
					list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
					$textcolor = imagecolorallocate($bigImg, 255, 140, 25);
					imagettftext($bigImg, 40, 0, 248, 550, $textcolor, public_path() . '/myfont.ttf', $user['invite_code']);
					imagepng($bigImg, public_path() . "/upload/invite/{$user['id']}.png");
					DB::commit();
					Cache::pull(request()->username);
					$users = UserModel::where('id',$user['id'])->first();
				}
				
				$data['list']['id'] = $users['id'];
				$data['list']['token'] = $users['token'];
				$data['list']['user'] = $users['user'];
				$data['list']['is_auth'] = $users['is_auth'];
				$data['list']['invite_code'] = $users['invite_code'];
				//用户是否第三方
				$party = UserLoginModel::where('uid',$users['id'])->first();
				if($party['picname']){
					$data['list']['picname'] = $party['picname'];
				}else{
					if(empty($users['picname'])){
						$data['list']['picname'] = "http://".$_SERVER['HTTP_HOST']."/user.png";
					}else{
						$data['list']['picname'] = "http://".$_SERVER['HTTP_HOST'].$users['picname'];
					}
				}
				$data['is_info'] = 1;
				return ['code' => 1, 'msg' => retStrInfo(11),'data'=>$data];
			} catch (Expectation $e) {
				DB::rollback();
				return ['code' => 0, 'msg' => retStrInfo(12)];
			}
		}else{
			//微信，QQ第三方登录
			$user_login = UserLoginModel::where('openid',request()->openid)->where('type',request()->type)->first();
			$data['is_info'] = 2;
			$data['list']['id'] = 0;
			$data['list']['token'] = '';
			$data['list']['user'] = '';
			$data['list']['is_auth'] = 0;
			$data['list']['invite_code'] = '';
			$data['list']['picname'] = "";

			//判断是否存在用户信息
			if($user_login){
				$data['is_info'] = 1;
				//用户信息
				$user = UserModel::where('id',$user_login['uid'])->first();
				//更改token
				$user->generateToken();
				$data['list']['id'] = $user['id'];
				$data['list']['token'] = $user['token'];
				$data['list']['user'] = $user['user'];
				$data['list']['is_auth'] = $user['is_auth'];
				$data['list']['invite_code'] = $user['invite_code'];
				//用户是否第三方
				$party = UserLoginModel::where('uid',$user['id'])->first();
				$data['list']['picname'] = $party['picname'];

			}
			return ['code'=>1,'msg'=>'返回数据','data'=>$data];
		}
	}

	//第三方注册
	public function qq_info_add(){
		//openid为空
		if(!request()->openid){
			return ['code'=>0,'msg'=>retStrInfo(115)];
		}
		$phone = '';
		$email = '';
		//判断账户/邮箱格式是否正确
		$str_phone = '/^1[3456789]\d{9}$/';
		$str_email = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/';
		if(request()->type == 1){
			if (!preg_match($str_phone, request()->username)) {
				return ['code' => 0, 'msg' => retStrInfo(4)];
			}
			$phone = request()->username;
			$user = UserModel::where('phone', request()->username)->first();
		}else{
			if (!preg_match($str_email, request()->username)) {
				return ['code' => 0, 'msg' => retStrInfo(5)];
			}
			$user = UserModel::where('email', request()->username)->first();
			$email = request()->username;
		}
		//判断该账户是否存在
		if ($user) {

			//判断是否第三方绑定
			$is_exit = UserLoginModel::where('uid',$user['id'])->where('type',request()->partyid)->first();
			if($is_exit){
				return ['code' => 0, 'msg' => retStrInfo(6)];
			}else{
				UserLoginModel::create([
					'uid' => $user->id,
					'openid' => request()->openid,
					'type' => request()->partyid,
					'picname' => request()->picname,
				]);
				$data['id'] = $user['id'];
				$data['token'] = $user['token'];
				$data['user'] = $user['user'];
				$data['is_auth'] = $user['is_auth'];
				$data['invite_code'] = $user['invite_code'];
				//用户是否第三方
//				$party = UserLoginModel::where('uid',$user['id'])->first();
				$data['picname'] = request()->picname;
				return ['code' => 1, 'msg' => retStrInfo(11),'data'=>$data];
			}
		}
		//云仓号
		$pid = 0;
		$path = '0,';
		if(request()->invite_code){
			$puser = UserModel::where('invite_code', request()->invite_code)->first();
			if (!$puser) {
				return ['code' => 0, 'msg' => retStrInfo(1)];
			}
			$pid = $puser['id'];
			$path = $puser['path'].$pid.",";
		}
		//验证码
		$str = '/^\\d{6}$/';
		if (!preg_match($str, request()->code)) {
			return ['code' => 0, 'msg' => retStrInfo(7)];
		}
		$code = Cache::get(request()->username);
		if ($code != request()->code) {
			return ['code' => 0, 'msg' => retStrInfo(8)];
		}

		//密码
		if (!request()->password) {
			return ['code' => 0, 'msg' => retStrInfo(9)];
		}
		//支付密码
		if (!preg_match($str, request()->paypass)) {
			return ['code' => 0, 'msg' => retStrInfo(10)];
		}
		//区号
		if(request()->area_code){
			$area_code = request()->area_code;
		}else{
			$area_code = '+86';
		}
		//第三方登录方式
		if(!request()->partyid){
			return ['code' => 0, 'msg' => retStrInfo(118)];
		}

		DB::beginTransaction();
		try {
			if(request()->invite_code){
				$pids = explode(',',$path);
				foreach($pids as $k=>$v){
					UserModel::where('id',$v)->increment('team');
				}
				UserModel::where('id',$pid)->increment('push');
			}
			do {
				$invite_cy = UserModel::orderBy('id','desc')->first();
				$invite_code = zero($invite_cy['invite_code'] +1);
	
			} while (UserModel::where('invite_code', $invite_code)->first());
			do {
				//昵称
				$user_cy = $invite_code;
			} while (UserModel::where('user', $user_cy)->first());

			$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
			if (preg_match($auth_name, $user_cy)) {
				$pin = app('pinyin');
				$pinyin =$pin->abbr($user_cy);
			}else{
				$pinyin = $user_cy;
			}
			//首字母
			$head = pinying($pinyin);

			$user = UserModel::create([
				'phone' => $phone,
				'email' => $email,
				'password' => Hash::make(request()->password),
				'paypass' => Hash::make(request()->paypass),
				'pid' => $pid,
				'path' => $path,
				'user' => $user_cy,
				'invite_code' => $invite_code,
				'area_code' => $area_code,
				'login_type' => request()->type,
				'head' => $head,
			]);
			$user->generateToken();
			UserWalletModel::create([
				'uid'=>$user->id,
			]);
			UserLoginModel::create([
				'uid' => $user->id,
				'openid' => request()->openid,
				'type' => request()->partyid,
				'picname' => request()->picname,
			]);

			$img = $user['invite_code'].'.png';
			$qrCode = new \Endroid\QrCode\QrCode('http://' . request()->getHost() . '/invite_own?invite=' . $user['invite_code']);
			$qrCode->setSize(300);
			$qrCode->writeFile(public_path() . "/upload/invite/".$img);
			$bigImgPath = public_path() . '/upload/invite/invite.png';
			$qCodePath = public_path() . "/upload/invite/".$img;
			$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
			$qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
			list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
			imagecopymerge($bigImg, $qCodeImg, 225, 680, 0, 0, $qCodeWidth, $qCodeHight, 100);
			list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
			$textcolor = imagecolorallocate($bigImg, 255, 140, 25);
			imagettftext($bigImg,  52, 0, 265, 600, $textcolor, public_path() . '/myfont.ttf', $user['invite_code']);
			imagepng($bigImg, public_path() . "/upload/invite/".$img);

			DB::commit();
			Cache::pull(request()->username);
			$users = UserModel::where('id',$user['id'])->first();
			$data['id'] = $users['id'];
			$data['token'] = $users['token'];
			$data['user'] = $users['user'];
			$data['is_auth'] = $users['is_auth'];
			$data['invite_code'] = $users['invite_code'];
			//用户是否第三方
			$party = UserLoginModel::where('uid',$user['id'])->first();
			if($party['picname']){
				$data['picname'] = $party['picname'];
			}else{
				if(empty($users['picname'])){
					$data['picname'] = "http://".$_SERVER['HTTP_HOST']."/user.png";
				}else{
					$data['picname'] = "http://".$_SERVER['HTTP_HOST'].$users['picname'];
				}
			}

			return ['code' => 1, 'msg' => retStrInfo(11),'data'=>$data];
		} catch (Expectation $e) {
			DB::rollback();
			return ['code' => 0, 'msg' => retStrInfo(12)];
		}

	}
}